import libtcodpy as libtcod

class FOV(object):
    def __init__(self, map, lights, radius=8, algo=2):
        self.radius = radius
        self.algo = algo

        self.height, self.width = len(map), len(map[0])
        self.map = libtcod.map_new(self.width, self.height)   # Player's map
        for y in xrange(self.height):
            for x in xrange(self.width):
                libtcod.map_set_properties(self.map, x, y,
                                           map[y][x].transparent,
                                           map[y][x].passable)
        self.lights = []
        for light in lights:
            light_map = libtcod.map_new(self.width, self.height)
            libtcod.map_copy(self.map, light_map)
            self.lights.append((light, light_map))



    def __contains__(self, pos):
        if libtcod.map_is_in_fov(self.map, pos[0], pos[1]):
            return True

    def in_static(self, x, y):
        for light, map in self.lights:
            if libtcod.map_is_in_fov(map, x, y):
                return True

        return False

    def compute_static(self):
        algo = 2
        for light, map in self.lights:
            libtcod.map_compute_fov(map, light.x, light.y, light.radius, True, algo)

    def compute(self, x, y):
        libtcod.map_compute_fov(self.map, x, y, self.radius, True, self.algo)

    @property
    def static_map(self):
        self.compute_static()

        map = []
        for y in range(self.height):
            row = []
            for x in range(self.width):
                row.append(self.in_static(x, y))
            map.append(row)

        return map

